package com.tensquare.recruit.service;

import com.tensquare.recruit.dao.EnterpriseDao;
import com.tensquare.recruit.pojo.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 服务层
 */

@Service
public class EnterpriseService {

    @Autowired
    private EnterpriseDao enterpriseDao;

    @Autowired
    private IdWorker idWorker;

    //三个查询
    public List<Enterprise> findAll(){
        return enterpriseDao.findAll();
    }

    public List<Enterprise> findSearch(Map whereMap){
        Specification<Enterprise> specification = createSpecification(whereMap);
        return enterpriseDao.findAll(specification);
    }

     //条件查询+分页
    public Page<Enterprise> findSearch(Map whereMap,int page,int size){
        Specification<Enterprise> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page-1,size);
        return enterpriseDao.findAll(specification,pageRequest);
    }

    public Object findById(String id) {
       return enterpriseDao.findById(id).get();
    }


    public void add(Enterprise enterprise) {
        enterprise.setId(idWorker.nextId()+"");
        enterpriseDao.save(enterprise);
    }

    public void deleteById(String id) {
        enterpriseDao.deleteById(id);
    }

    public void update(Enterprise enterprise) {
        enterpriseDao.save(enterprise);
    }


    /**
     * 动态条件构建
     *
     *
     * if (null != searchMap.get("")&& !"".equals(searchMap.get(""))){
     *                     predicateList.add(cb.like(root.get("").as(String.class),"%"+(String)searchMap.get("")+"%"));
     *                 }
     */

    private Specification<Enterprise> createSpecification(Map searchMap){
        return new Specification<Enterprise>() {
            @Override
            public Predicate toPredicate(Root<Enterprise> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

                List<Predicate> predicateList = new ArrayList<Predicate>();
                //ID
                if (null != searchMap.get("id")&& !"".equals(searchMap.get("id"))){
                    predicateList.add(cb.like(root.get("id").as(String.class),"%"+(String)searchMap.get("id")+"%"));
                      }
                //企业名称
                if (null != searchMap.get("name")&& !"".equals(searchMap.get("name"))){
                    predicateList.add(cb.like(root.get("name").as(String.class),"%"+(String)searchMap.get("name")+"%"));
                }
                //企业简介
                if (null != searchMap.get("summary")&& !"".equals(searchMap.get("summary"))){
                    predicateList.add(cb.like(root.get("summary").as(String.class),"%"+(String)searchMap.get("summary")+"%"));
                }
                //企业地址
                if (null != searchMap.get("address")&& !"".equals(searchMap.get("address"))){
                    predicateList.add(cb.like(root.get("address").as(String.class),"%"+(String)searchMap.get("address")+"%"));
                }
                //标签列表
                if (null != searchMap.get("labels")&& !"".equals(searchMap.get("labels"))){
                    predicateList.add(cb.like(root.get("labels").as(String.class),"%"+(String)searchMap.get("labels")+"%"));
                }
                //坐标
                if (null != searchMap.get("coordinate")&& !"".equals(searchMap.get("coordinate"))){
                    predicateList.add(cb.like(root.get("coordinate").as(String.class),"%"+(String)searchMap.get("coordinate")+"%"));
                }
                //是否热门
                if (null != searchMap.get("ishot")&& !"".equals(searchMap.get("ishot"))){
                    predicateList.add(cb.like(root.get("ishot").as(String.class),"%"+(String)searchMap.get("ishot")+"%"));
                }
                //LOGO
                if (null != searchMap.get("logo")&& !"".equals(searchMap.get("logo"))){
                    predicateList.add(cb.like(root.get("logo").as(String.class),"%"+(String)searchMap.get("logo")+"%"));
                }
                //URL
                if (null != searchMap.get("url")&& !"".equals(searchMap.get("url"))){
                    predicateList.add(cb.like(root.get("url").as(String.class),"%"+(String)searchMap.get("url")+"%"));
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }



}
